/*******************************************************************/
/* @ All Rights Reserved Copyright (C) 2014 broada,Co.,Ltd.        */
/*                                                                 */
/*  NAME      = t__jolt_call_resp.c                                */
/*  NOTE      =                                                    */
/*  DATE      = 2016/03/04 by songbb                               */
/*******************************************************************/

#include <CUnit/Basic.h>
#include <stdlib.h>

#include "frcomm.h"
#include "frmem.h"
#include "frct_comm.h"

#include "../../../src/module/tuxedojolt/config.h"
#include "../test_jolt_comm.h"
#include <tuxedojolt/jolt_core.h>
#include <tuxedojolt/jolt_comm.h>
#include <frct_comm.h>
#include <apmtest_comm.h>
#include <tuxedojolt/jolt_packet.h>
#include <frreport_comm.h>
#include <frreport_opt.h>
#include <tuxedojolt/jolt_dataxfer.h>

extern int __jolt_call_resp(Conntrack *p_st_conn
                            , L7session_jolt *p_st_sess
                            , L7_jolt_packet *p_st_jolt_packet
                            , Frhash_header *p_st_hash_report);

static int __suite_init(void)
{
    return 0;
}

static int __suite_cleanup(void)
{
    return 0;
}

static void __case1(void)
{
    Conntrack *p_st_conn = NULL;
    CU_ASSERT_EQUAL(test_jolt_create_ct(&p_st_conn, TEST_JOLT_SRC_IP, TEST_JOLT_DST_IP, TEST_JOLT_SRC_PORT, TEST_JOLT_DST_PORT), FR_SUCC);

    L7_session *p_st_session = frct_get_session(p_st_conn);

    L7session_jolt_private *p_st_session_private = (L7session_jolt_private *) frct_get_session_private(p_st_session);
    L7session_jolt *p_st_sess = p_st_session_private->p_st_sess;
    L7_jolt_packet *p_st_jolt_packet = p_st_session_private->p_st_jolt_packet;

    char *p_bin = (char *) "4a4f4c54000000000000006f000000000000008d0000002f00000001000000000000000400000000000000000000000000000000000000000000000000000000000100c90e000009000000130a000001000000000a48454c4c4f574f524c4402000005000000000200000c00000000";
    char arr_payload[65536] = {'\0'};

    CU_ASSERT_EQUAL(apmtest_hex_string_to_bin(p_bin, arr_payload, sizeof(arr_payload)), FR_SUCC);

    unsigned int ui_payload_len = strlen(p_bin) / 2;
    Apm_packet *p_st_packet = NULL;
    struct timeval st_timestamp;
    st_timestamp.tv_sec = 1454549354L;
    st_timestamp.tv_usec = 123456L;
    CU_ASSERT_EQUAL(apmtest_create_apm_packet(&p_st_packet, arr_payload, ui_payload_len, TEST_JOLT_SRC_IP, TEST_JOLT_DST_IP, TEST_JOLT_SRC_PORT, TEST_JOLT_DST_PORT, st_timestamp), FR_SUCC);
    p_st_packet->i_direction = FR_DIRECT_SRV2CLIENT;

    CU_ASSERT_EQUAL(jolt_packet_unfragment(p_st_sess, p_st_packet, p_st_jolt_packet), APM_JOLT_CHECK_FRAGMENT_SUCCESS);

    Frhash_header st_hash_report;
    CU_ASSERT_EQUAL(frhash2_create(&st_hash_report, 2), FR_SUCC);

    p_st_session_private->p_report_comm = frlist_create();
    CU_ASSERT_PTR_NOT_NULL(p_st_session_private->p_report_comm);
    p_st_session_private->p_report_req = frlist_create();
    CU_ASSERT_PTR_NOT_NULL(p_st_session_private->p_report_req);

    p_st_session_private->p_st_jolt_stmt->ui_msg_id = 4;

    CU_ASSERT_EQUAL(bytebuff_skip(p_st_session_private->p_st_jolt_packet->p_st_packet, APM_JOLT_PKT_HEADER_LEN + 4), FR_SUCC);

    CU_ASSERT_EQUAL(__jolt_call_resp(p_st_conn, p_st_sess, p_st_jolt_packet, &st_hash_report), FR_SUCC);

    Frvar_field *p_st_field = NULL;
    Frlist_header *p_list_report = NULL;
    Frlist_node *p_st_node_report = NULL;
    Frreport_data *p_st_report_data = NULL;
    p_list_report = &((&st_hash_report)->p_list_array[0]);

    p_st_node_report = FRLIST_FIRST_NODE(p_list_report);
    CU_ASSERT_PTR_NOT_NULL(p_st_node_report);

    p_st_report_data = (Frreport_data *) FRLIST_GET_NODE_DATA(p_st_node_report);
    CU_ASSERT_PTR_NOT_NULL(p_st_report_data);

    p_st_field = FrReportOpt_get_field(p_st_report_data, APM_REPORT_KEY_RESP_CODE, 0);
    CU_ASSERT_EQUAL(p_st_field->em_data_type, FRLONG);
    CU_ASSERT_EQUAL(p_st_field->field_value.ll_value, 0L);

    p_st_field = FrReportOpt_get_field(p_st_report_data, APM_REPORT_KEY_RESP_CODE_STRING, 0);
    CU_ASSERT_PTR_NULL(p_st_field);

    p_st_field = FrReportOpt_get_field(p_st_report_data, APM_DTS_REPORT_RESP_ARG_SIZE, 0);
    CU_ASSERT_EQUAL(p_st_field->em_data_type, FRLONG);
    CU_ASSERT_EQUAL(p_st_field->field_value.ll_value, 1L);

    p_st_field = FrReportOpt_get_field(p_st_report_data, APM_DTS_REPORT_RESP_ARG_DATA"1", 0);
    CU_ASSERT_EQUAL(p_st_field->em_data_type, FRSTRING);
    CU_ASSERT_STRING_EQUAL(p_st_field->field_value.p_value, "HELLOWORLD");

    frhash2_free(&st_hash_report, list_report_node_free);

    apmtest_destroy_apm_packet(&p_st_packet);
    CU_ASSERT_PTR_NULL(p_st_packet);

    test_jolt_destroy_ct(&p_st_conn);
    CU_ASSERT_PTR_NULL(p_st_conn);
}

int add_suite__jolt_call_resp(void)
{
    CU_pSuite pSuite = NULL;

    if ((pSuite = CU_add_suite(MODULE_NAME" __jolt_call_resp()", __suite_init, __suite_cleanup)) == NULL)
        return FR_FAIL;

    if (CU_add_test(pSuite, "case1", __case1) == NULL)
        return FR_FAIL;

    return FR_SUCC;
}
